﻿layui.define(['index', 'element', 'lockscreen'], function (exports) {
    var $ = layui.$,
        setter = layui.setter,
        element = layui.element,
        admin = layui.admin,
        router = layui.router(),
        view = layui.view,
        lockscreen = layui.lockscreen;
    var html = [];
    if (!layui.sessionData(setter.tableName).hasOwnProperty('UserInfo')) {
        admin.exit();
    }
    //IE提示
    if (layui.device().ie) {
        view.error('暂不支持IE访问，推荐使用：Edge/Chrome/Firefox/ 等高级浏览器', {
            //offset: 'auto',
            id: 'LAY_errorIE'
        });
    }
    $(".ws-menu-bar").on("click", function () {
        $("body").addClass("ws-menu-show ws-shade-show");
        $(".layui-side-menu").addClass("ws-menu");
    });
    $(".ws-shade").on("click", function () {
        $("body").removeClass("ws-menu-show ws-shade-show");
        $(".layui-side-menu").removeClass("ws-menu");
    })

    if (layui.sessionData(setter.tableName)["UserInfo"]) {
        $("#adminName cite")[0].innerHTML = layui.sessionData(setter.tableName)["UserInfo"].user.cName;
    }
    var randerTree = function (d) {
        layui.each(d, function (index, item) {
            html.push(' <dd>');
            if (item.children.length > 0) {
                html.push(' <a href="javascript:;" lay-direction="2">');
                if (item.icon.startsWith("fa")) {
                    html.push('<i class="fa ' + item.icon + ' fa-fw" aria-hidden="true"></i>');
                } else {
                    html.push('<i class="layui-icon ' + item.icon + '"></i>');
                }
                html.push(' <cite>' + item.name + '</cite>');
                html.push('</a>');
            } else {
                html.push('<a data-moduleId="' + item.id + '" data-moduleName="' + item.name + '" lay-href="' + item.compPath + '">');
                if (item.icon.startsWith("fa")) {
                    html.push('<i class="fa ' + item.icon + ' fa-fw" aria-hidden="true"></i>');
                } else {
                    html.push('<i class="layui-icon ' + item.icon + '"></i>');
                }
                html.push(item.name + '</a>');
            }
            if (item.children.length > 0) {
                html.push(' <dl class="layui-nav-child">');
                randerTree(item.children);
                html.push(' </dl>');
            }
            html.push(' </dd>');
        })
    }
    //左侧菜单动态赋值
    if (layui.sessionData(setter.tableName)["UserInfo"]) {
        //html.push('<div class="layui-logo" lay-href="views/home/homepage1.html" style="user-select:none;"><span>通用管理后台</span></div>');
        html.push('<ul class="layui-nav layui-nav-tree" lay-shrink="all" id="LAY-system-side-menu" lay-filter="layadmin-system-side-menu">');

        html.push('<li data-name="home" class="layui-nav-item">');
        html.push('<a href="javascript:;" lay-tips="主页" lay-direction="2">');
        html.push('<i class="layui-icon layui-icon-home"></i>');
        html.push('<cite>主页</cite>');
        html.push('<i class="layui-icon layui-icon-down layui-nav-more"></i></a>');
        html.push('<dl class="layui-nav-child" > ');
        html.push('<dd data-name="console" class="layui-this" > ');
        html.push('<a lay-href="views/home/homepage1.html" data-moduleId="000000"> 控制台</a > ');
        html.push('</dd > ');
        html.push('<dd data-name="console" > ');
        html.push('<a lay-href="views/home/homepage2.html" data-moduleId="111111" data-moduleName="主页一"> 主页一</a > ');
        html.push('</dd > ');
        html.push('<dd data-name="console" > ');
        html.push('<a lay-href="views/home/homepage3.html" data-moduleId="222222" data-moduleName="主页二"> 主页二</a > ');
        html.push('</dd > ');
        html.push('</dl > ');
        html.push('</li> ');

        layui.each(layui.sessionData(setter.tableName)["UserInfo"].modules, function (index, item) {
            html.push(' <li data-name="senior" class="layui-nav-item">');
            html.push(' <a href="javascript:;" lay-direction="2">');
            if (item.icon.startsWith("fa")) {
                html.push('<i class="fa ' + item.icon + ' fa-fw" aria-hidden="true"></i>');
            } else {
                html.push('<i class="layui-icon ' + item.icon + '"></i>');
            }
            html.push(' <cite>' + item.name + '</cite>');
            html.push('</a>');
            html.push(' <dl class="layui-nav-child">');
            randerTree(item.children);
            html.push(' </dl>');
        });
        html.push('</ul></div> ');
        $("#menu-nav").html(html.join(''));
        element.init();
    }
    else {
        view.exit();
    }
    var openedTabs = layui.sessionData(setter.tableName)["OpenTabs"] || { tabs: [], tabsPage: {} };
    openedTabs.tabs.forEach(function (item, index) {
        $("#LAY_app_body").append([
            '<div class="layadmin-tabsbody-item ">', `<iframe loading="lazy" src="${item.attr}?v=${setter.version}" name="${index + 1}" data-moduleId="${item.moduleid}" data-moduleName="${item.modulename}"` + ' frameborder="0" class="layadmin-iframe"></iframe>', '</div>'
        ].join(''));
        element.tabAdd('layadmin-layout-tabs', {
            title: '<span style="">' + item.title_text + '</span>',
            id: item.id,
            attr: item.attr
        });
    });

    admin.tabsPage = openedTabs.tabsPage;
    if (openedTabs.tabsPage && Object.keys(openedTabs.tabsPage).length !== 0) {
        element.tabChange('layadmin-layout-tabs', openedTabs.tabsPage.dataset.moduleid);
        admin.tabsBodyChange(openedTabs.tabsPage.index, {
            url: openedTabs.tabsPage.url,
            text: openedTabs.tabsPage.text
        });
    }
    if (window.name == '') {
        admin.req({
            type: 'post',
            url: '/api/Login/IsLogin',
            success: function () { }
        })
        window.name = 'reload'
    }
    admin.req({
        type: 'get',
        url: '/api/UserMsg/GetUnreadMsg',
        success: function (r) {
            $("#LAY_app").show();
            if (r.data.length == 0) {
                $("#isUnread").removeClass('layui-badge-dot');
            }
        }
    })
    window.chatHub(layui.layer, view, layui);

    //对外输出
    exports('home', {});
})